;
(function ($) {
	$.extend({
		DetailPages: {

			/* 从location中获取id */
			getQueryParams: function (key) {


				if (!location.search) return null
				var str = location.search.substring(1)
				var arr = str.split('&')
				if (arr.length === 0) return null
				for (var i = 0; i < arr.length; i++) {
					var itemarr = arr[i].split('=')
					if (itemarr[0] === key) {
						return itemarr[1]
					}
				}


			},

			/* 以下为登陆状态后，立即购买存cookie */
			buyCookie: function (thisgoodsid, userid, Lijishopcarlist) {
				$(".buy-button").click(function () {
					let shopcarcount = parseInt($("#count").val())
					let id = parseInt(thisgoodsid)
					if (userid) {

						var isAlive = false
						for (var i = 0; i < Lijishopcarlist.length; i++) {
							if (Lijishopcarlist[i].id === id) {
								Lijishopcarlist[i].count = shopcarcount;
								isAlive = true
								break
							}
						}
						if (!isAlive) {
							Lijishopcarlist.push({
								id: id,
								count: shopcarcount
							})
						}
						$.cookie("Lijishopcarlist", JSON.stringify(Lijishopcarlist))
						javascript: window.open("shopcarjiesuan.html", "_blank")
					} else (
						$("body").alertLogin(`DetailPages.html?id=${id}`)
					)

				})
			},

			/* 以下代码操作购物车cookie和后台数据 */
			usercookie: function (userid) {
				if (userid) { //如果登陆了，用后台购物车的数据覆盖cookie
					$.get("/danduloadshoplist", {
						id: userid
					}, function (data) {
						$.cookie("shopcarList", JSON.stringify(data))
					})
				}
			},

			/* 购物车弹出框 */
			shopcaralert: function (thisgoodsid, userid) {
				layui.use('layer', function () {
					var layer = layui.layer;
					$(".join_button").click(function () {
						let shopcarcount = parseInt($("#count").val())
						$.get("/goods", {
							id: thisgoodsid
						}, function (data) {
							if ($("#count").val() > data.stock) {
								layer.msg('当前库存不足！')
							} else {
								
								var shopcarlist = []
								if ($.cookie("shopcarList")) {
									shopcarlist = JSON.parse($.cookie("shopcarList"))
								}
								
								let id = parseInt(thisgoodsid)
								var isAlive = false
								for (var i = 0; i < shopcarlist.length; i++) {
									if (shopcarlist[i].goosid === id) {
										shopcarlist[i].count=parseInt(shopcarlist[i].count)
										shopcarlist[i].count += parseInt(shopcarcount);
										isAlive = true
										if (userid) {
											//如果是同样商品，更新商品数量
											$.post("/updatecount", {
												id: id,
												count: shopcarlist[i].count,
												userid: userid
											}, function (data) {

											})
										}
										break
									}
								}
								if (!isAlive) {
									shopcarlist.push({
										goosid: id,
										count: shopcarcount
									})
									if (userid) {
										//后台添加购物车数据接口
										$.post("/addshopcarlist", {
											id: id,
											count: shopcarcount,
											userid: userid
										}, function (data) {

										})
									}
								}

								$.cookie("shopcarList", JSON.stringify(shopcarlist), {
									expires: 60
								})
								layer.open({
									type: 1,
									title: 0,
									content: $("#detail_content"),
									area: ['420px', '240px']
								});


							}

						})


					})
					$(".detail_info_no").click(function () {
						layer.closeAll('page')
					})
				});
			},

			/* 点击显示分享 */
			sharebtn: function () {
				$(".Detail-box-info").on("click", ".Detail-box-share-btn", function () {
					if ($(".Detail-box-code").hasClass("show")) {
						$(".Detail-box-code").hide().removeClass("show");
					} else {
						$(".Detail-box-code").show().addClass("show");
					}
				})
			},

			/* 页面打开时的喜欢状态接口 */
			goodslike: function (userid, thisgoodsid) {
				var status = false;
				$.get("/goodslikeif", {
					goodsid: thisgoodsid,
					userid: userid
				}, function (data) {
					if (data.msg) {
						$(".Detail_box_like").find("i").css("background-position", "-166px -45px")
						status = false;
					} else {
						$(".Detail_box_like").find("i").css("background-position", "-189px -45px")
						status = true;
					}
				})


				/* 点击判断是否喜欢收藏 */
				var status = true;
				let id = parseInt(thisgoodsid)
				$(".Detail_box_like").click(function () {
					if ($.cookie("userId")) {
						if (status) {
							$(this).find("i").css("background-position", "-166px -45px")
							status = false;
							$.post("/goodslikeadd", {
								goodsid: thisgoodsid,
								userid: userid
							})

						} else {
							$(this).find("i").css("background-position", "-189px -45px")
							status = true;
							$.post("/goodslikedel", {
								goodsid: thisgoodsid,
								userid: userid
							})
						}
					} else {
						$("body").alertLogin(`DetailPages.html?id=${id}`)
					}
				})
			},

			/* 点击数量加减 */
			goodscount: function () {
				var jian = document.getElementById("cut");
				var jia = document.getElementById("add");
				var count = document.getElementById("count");
				jia.onclick = function () {
					count.value++;
				}
				jian.onclick = function () {
					if (count.value <= 1) {
						count.value = 1;

					} else {
						count.value--;
					}
				}
			},

			/* 点击小图片切换相应大图 */
			imgchange: function () {
				$(".Detail-box-album").on("click", ".Detail-small-img", function () {
					let src = $(this).attr("src")
					$(".Detail-big-img").attr("src", src)
				})
			},

			/* 详情页面包屑接口 */
			breadnav: function (thisgoodsid) {
				$.get("/goodsbreadnav", {
					id: thisgoodsid
				}, function (data) {

					var strr = `
					  <li>
						<div class="Detail-icon-to"></div>
					  </li>
					`
					if (thisgoodsid < 24) {
						strr += `
					<li>
						<a href="listPage.html" target="_blank">${data.firsttitle}</a>
					</li>
					<li>
						<div class="Detail-icon-to"></div>
					</li>
						`
					} else {
						strr += `
					<li>
						<a href="listPage2.html" target="_blank">${data.firsttitle}</a>
					</li>
					<li>
						<div class="Detail-icon-to"></div>
					</li>
						`
					}

					strr += `
					  <li>
						<a href="listPage-circums.html?id=${data.id}" target="_blank">${data.secondtitle}</a>
					</li>
					<li>
						<div class="Detail-icon-to"></div>
					</li>
					<li class="DetailPage">
						${data.thirdtitle}
					</li>
					`

					$(".Detail-index").append(strr)


				})
			},

			/* 详情页标题价格接口 */
			detailtitle: function (thisgoodsid) {
				$.get("/goods", {
					id: thisgoodsid
				}, function (data) {
					$(".Detail").append(
						`<div class="Detail-box-info-hava row">
							<div class="Detail-box-info-tag">${data.type}</div>
							<div class="Detail-box-info-title">${data.title}</div>
							<a href="DetailPages.html?id=5" target="_blank">暴风魔镜Matrix 官方标准礼包</a>
						</div>
						<p class="Detail-box-title row">${data.content}</p>
						<!-- 价格 -->
						<div class="Detail-box-price row">
							<label>价格</label>
							<div class="Detail-box-form">
								<strong>${data.unitPrice}
									<span>元</span>
								</strong>
								<span class="Detail-box-product-price">市场价：
									<span>${data.oldPrice}</span>元
								</span>
								<span class="Detail-box-savings-money">
									已省：<span>${data.oldPrice - data.unitPrice}</span>元
								</span>
								<div class="Detail-box-share-btn">
									<i></i>分享好物
									<div class="Detail-box-code show">
										<i></i>
										<img src="img/qrcode.1.png">
										<p>微信扫一扫 分享给好友</p>
									</div>
								</div>
							</div>
						</div>`
					)
				})
			},

			/* 详情页小图片接口 */
			smallimg: function (thisgoodsid) {
				$.get("/goods_title_img", {
					id: thisgoodsid
				}, function (data) {
					let titleimg = "";
					titleimg +=
						`<div class="Detail-box-big-img" >
						<img src="${data[0].imgadd}" class ="Detail-big-img">
					</div>
					<div class="Detail-box-small-imgs row">
						<div class="Detail-small-imgs-silder">
							<ul>`
					data.forEach(function (item) {
						titleimg += `<li >
									<img src="${item.imgadd}" class="Detail-small-img">
								</li>`
					})
					titleimg += `</ul>
						</div>
					</div>`
					$(".Detail-box-album").append(titleimg)
				})
			},

			/* 详情页介绍大图片接口 */
			bigimg: function (thisgoodsid) {
				$.get("/goods_desc_img", {
					id: thisgoodsid
				}, function (data) {
					let descimg = "";
					descimg +=
						`<div class="detail-text-right-title">
						<div class="detail-text-right-current">商品介绍</div>
					</div>
					<div class="detail-text-right-banner">`
					data.forEach(function (item) {
						descimg += `<img src="${item.imgadd}">`
					})
					descimg += `</div>`
					$(".detail-text-right").append(descimg)
				})
			},

			main: function () {
				$("#count").val(1)
				$.DetailPages.getQueryParams()
				var thisgoodsid = $.DetailPages.getQueryParams("id")
				let shopcarlist = []
				let Lijishopcarlist = []
				let userid = $.cookie("userId")
				$.DetailPages.buyCookie(thisgoodsid, userid, Lijishopcarlist)
				$.DetailPages.usercookie(userid)
				$.DetailPages.shopcaralert(thisgoodsid, userid)
				$.DetailPages.sharebtn()
				$.DetailPages.goodslike(userid, thisgoodsid)
				$.DetailPages.goodscount()
				$.DetailPages.imgchange()
				$.DetailPages.breadnav(thisgoodsid)
				$.DetailPages.detailtitle(thisgoodsid)
				$.DetailPages.smallimg(thisgoodsid)
				$.DetailPages.bigimg(thisgoodsid)
			}
		}
	})
})(jQuery)
